/* Includes ------------------------------------------------------------------ */
#include "usbd_msc_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "stm32f4xx_usart.h"

#include "fatfs_sd.h"

#include <stdio.h>
#include <string.h>

__ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END;

void USART1_init(void);

int main(void)
{
	/* !< At this stage the microcontroller clock setting is already configured,
	 * this is done through SystemInit() function which is called from startup
	 * file (startup_stm32fxxx_xx.s) before to branch to application main. To
	 * reconfigure the default setting of SystemInit() function, refer to
	 * system_stm32fxxx.c file */
	// USART_Config();
	USART1_init();
	
	printf("System started ...\r\n");

	// LCD_Init();
	// LCD_String(0, 0, "STM32F407ZGT6 OTG MSC", 24, LCD_COLOR_MAGENTA, LCD_COLOR_BLUE);
	
	SdCard_init();
	
	if (SD_disk_initialize(0) != 0) {
		printf("SD_disk_initialize failed !\r\n");
		return (-1);
	}

	// if (SD_GetStatus() != 0) {
	// 	return (-1);
	// }
	printf("Start USB ...\r\n");
	USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_MSC_cb);

	while (1) {

	}
}

/**
  * @}
  */

void USART1_init(void)
{
	/* Configure GPIO */
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Enable GPIO clock */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

	/* Enable UART clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

	/* Configure USART Tx Rx as alternate function  */
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	/* Connect PXx to USARTx_Tx*/
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

	/* Configure USART */
	USART_InitTypeDef USART_InitStructure;

	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	/* USART configuration */
	USART_Init(USART1, &USART_InitStructure);

	/* Enable USART */
	USART_Cmd(USART1, ENABLE);

}

int fputc(int ch, FILE *f) {
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	USART1->SR;
	// while((USART1->SR&0X40)==0);

	USART_SendData(USART1, (uint16_t) ch);

	/* Loop until the end of transmission */
	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
	{}

	return ch;
}

#ifdef USE_FULL_ASSERT
/**
* @brief  assert_failed
*         Reports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @param  File: pointer to the source file name
* @param  Line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
	/* User can add his own implementation to report the file name and line
	 * number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file,
	 * line) */

	/* Infinite loop */
	while (1) {
	}
}
#endif



/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
